<template>
	<view>
		<cu-custom  bgColor="bg-black-two"  :isBack="true">
			<block slot="content">选择发票抬头</block>
		</cu-custom>
		<view>
			<view style="margin:10px;background-color: #2D2D3D;border-radius: 5px;height: 100%;padding-top: 15px;padding-bottom: 15px;">
				<view v-for="(item,index) in detailList" :key="index" 
				@click="getInfo(item.id,item.clientName,item.codeNo)" :ref="item.id" :class="selectInvoiceId == item.id ? 'selected-bg' : ''" >
					<view class="flex" style="padding: 15px 15px 0px 15px;">
						<view style="padding-right: 10px;">
							<text class="cuIcon-form"
								style="color: #00A99D;font-size: 30px; font-weight: bold;"></text>
						</view>
						<view>
							<view :class="selectInvoiceId == item.id ? 'selected-bg' : ''"
								style="color:#fff;font-size: 14px;">{{item.header}}</view>
							<view :class="selectInvoiceId == item.id ? 'selected-bg' : ''"
								style="color:gray;padding-top: 5px;">
								<text>纳税号：</text>
								<text>{{item.tax_id}}</text>
							</view>
						</view>
						
					</view>
					
				</view>
			</view>
			<view class="padding flex justify-between" style="position: fixed; bottom: 0px;background-color: #FFFFFF;width: 100%;">
				<button  @click="linktoInvoice()" class="cu-btn  bg-grey loginout-btn" style="width: 30%;font-size: 20px;">+</button>
				<button  @click="buyInvoice()" class="cu-btn  bg-grey loginout-btn" style="width: 60%;">确认开票</button>
			</view>
		</view>
	</view>
</template>

<script>
	var _self;
	export default{
		data(){
			return{
				sourceData:[],
				jifen:0,//当前积分
				modalName:'',
				tixianNumber:'',//提现金额
				bankCode:'',//银行卡号
				detailList:[],//积分明细
				selectInvoiceId:0,
			}
		},
		onShow() {
			_self = this;
			//获取发票列表
			this.getInitData();
		},
		onLoad(option) {
			console.log(option);
			if(Object.keys(option).length != 0){
				this.sourceData = JSON.parse(option.moduleInfo);
			}
		},
		methods:{
			//获取当前用户的余额和积分
	async	getInitData(){
				uni.showLoading({ title: '加载中...', mask: true });
				// 请求登录
				let [err,res] = await this.$http.get('invoiceList',{},{
					token:true,
					checkToken:true ,
				});
				uni.hideLoading();
				this.detailList = res.data.data;
			},
			//添加
			linktoInvoice(){
				let url = '/pages/center/fapiao/add';
				uni.navigateTo({
					url: url
				});
			},
			//获取选中的发票ID
			getInfo(id,strName,strNumber){
				this.selectInvoiceId = id;
			},
			//确认购买模块并开发票
			buyInvoice(){
				let paramsArray = {};
				paramsArray.user_id = uni.getStorageSync("userInfo").userID;//用户ID
				paramsArray.invoice_id = this.selectInvoiceId;//发票ID
				paramsArray.module_id = this.sourceData.id;//模块ID
				paramsArray.module_price = this.sourceData.module_price;//模块价格
				console.log(paramsArray);
				requestPost('buyModel',paramsArray,function(res){
					if (res.data.status !== "false") {						
						_self.getInitData();
						_self.modalName = null;
						uni.showToast({
						    //title: '开票成功，您已成功购买此模块，积分成功',
							title: '模块已解锁',
							icon:'success'
						});								
						setTimeout(function () {
						    uni.hideToast();
							uni.navigateBack({
								delta: 1
							});
						}, 2000);
					} else {
						uni.showToast({
						    //title: '开票成功，您已成功购买此模块，积分成功',
							title: '余额不足请充值',
							icon:'error'
						});		
					}
				});
			},
			
			showModal(name){
				this.modalName=name
			},
			hideModal() {
				this.modalName = null
			},

			
			//积分提现
			tixian(){				
				let paramsArray = {};
				paramsArray.user_id = uni.getStorageSync("userInfo").userID;
				paramsArray.number = this.tixianNumber;
				paramsArray.yinhangka = this.bankCode;
				requestPost('jifenExt',paramsArray,function(res){
					if (res.data.status !== "false") {
						_self.getInitData();
						_self.modalName = null;
					}
				});
			},
			//跳转页面
			linktoPage(urlStr){
				uni.navigateTo({
					url:urlStr
				})
			},
			cleanUserInfo(){
				let paramsArray = {};
				paramsArray.user_id = uni.getStorageSync("userInfo").userID;
				paramsArray.fapiaoId = this.selectInvoiceId;
				paramsArray.module_id = this.sourceData.id;
				requestPost('buyModel',paramsArray,function(res){
					if (res.data.status !== "false") {
						//提示购买成功 并返回上级页面
						
						_self.getInitData();
						_self.modalName = null;
					}else{
						uni.showToast({
							title:res.data.data,
							icon:'error'
						})
					}
				});
			},
			//充值-微信支付
			wxpayAuth(){
				uni.request({
					url: 'https://qmes.dascloud.cn/wxpayv3/?total=1',
					method: 'GET',
					header: {  
					    'Content-Type': 'application/x-www-form-urlencoded'  
					},
					//sslVerify: false,//本地模拟器调试
					success: ress => {
						console.log(ress.data);
						uni.getProvider({
							service: 'payment',
							success:function(res){
								console.log(res);
								if (~res.provider.indexOf('wxpay')) {
									uni.requestPayment({
										provider: 'wxpay',
										orderInfo: ress.data, //微信、支付宝订单数据
										success: function (res) {
											_self.modalName = null;
											_self.getInitData();//更新余额
											console.log('success:' + JSON.stringify(res));
										},
										fail: function (err) {
											console.log('fail:' + JSON.stringify(err));
										}
									});
								}
							}
						})
					}
				});
			
			},
		}
		
	}
</script>

<style>
	.my-bg{
		margin: 15px;background-color: #2D2D3D;border-radius: 5px;padding: 15px;
	}
	.my-title-bg{
		border-bottom: 1px solid #6A6A71;margin: 10px;padding: 0px 5px 5px 5px;
	}
	.loginout-btn{
		background-color:#00A99D;border-radius: 20px;
	}
	.uni-input-placeholder{
		color: #262626;
	}
	
	.selected-bg{
		background: #171723;
		color: #fff!important;
	}
	
</style>